package com.zaiteng;

import java.io.File;
import java.io.FileNotFoundException;

import org.json.JSONException;
import org.json.JSONObject;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.objects.collections.List;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

@ShortName("JMessage")
@Author("Icefairy333")
@Version(1.30f)
@Permissions(values={"android.permission.RECEIVE_USER_PRESENT","android.permission.INTERNET","android.permission.WAKE_LOCK","android.permission.READ_PHONE_STATE","android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE","android.permission.VIBRATE","android.permission.MOUNT_UNMOUNT_FILESYSTEMS","android.permission.ACCESS_NETWORK_STATE","android.permission.SYSTEM_ALERT_WINDOW","android.permission.WRITE_SETTINGS"})
@DependsOn(values={"jmessage-android-1.2.3"})
@Events(values={"onjmsg(msg as Message_im)","gotResult(eventtype as String,responseCode as Int,responseMessage as String,JsonStr as String)"})
public class JMsgWrapper {
	/**
	 * jmessage技术文档
	 * http://docs.jpush.io/guideline/jmessage_android_guide/
	 */
	public static final String ACTION_CONNECTION_CHANGE = "cn.jpush.android.intent.CONNECTION";
	public static final String ACTION_REGISTRATION_ID = "cn.jpush.android.intent.REGISTRATION";
	public static final String ACTION_STOPPUSH = "cn.jpush.android.intent.STOPPUSH";
	public static final String ACTION_RESTOREPUSH = "cn.jpush.android.intent.RESTOREPUSH";
	public static final String ACTION_MESSAGE_RECEIVED = "cn.jpush.android.intent.MESSAGE_RECEIVED";
	public static final String ACTION_NOTIFICATION_RECEIVED = "cn.jpush.android.intent.NOTIFICATION_RECEIVED";
	public static final String ACTION_NOTIFICATION_OPENED = "cn.jpush.android.intent.NOTIFICATION_OPENED";
	public static final String ACTION_NOTIFICATION_RECEIVED_PROXY = "cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY";
	public static final String ACTION_STATUS = "cn.jpush.android.intent.STATUS";
	public static final String ACTION_ACTIVITY_OPENDED = "cn.jpush.android.intent.ACTION_ACTIVITY_OPENDED";
	public static final String EXTRA_CONNECTION_CHANGE = "cn.jpush.android.CONNECTION_CHANGE";
	public static final String EXTRA_REGISTRATION_ID = "cn.jpush.android.REGISTRATION_ID";
	public static final String EXTRA_APP_KEY = "cn.jpush.android.APPKEY";
	public static final String EXTRA_NOTIFICATION_DEVELOPER_ARG0 = "cn.jpush.android.NOTIFICATION_DEVELOPER_ARG0";
	public static final String EXTRA_NOTIFICATION_TITLE = "cn.jpush.android.NOTIFICATION_CONTENT_TITLE";
	public static final String EXTRA_PUSH_ID = "cn.jpush.android.PUSH_ID";
	public static final String EXTRA_MSG_ID = "cn.jpush.android.MSG_ID";
	public static final String EXTRA_NOTI_TYPE = "cn.jpush.android.NOTIFICATION_TYPE";
	public static final String EXTRA_ALERT = "cn.jpush.android.ALERT";
	public static final String EXTRA_MESSAGE = "cn.jpush.android.MESSAGE";
	public static final String EXTRA_CONTENT_TYPE = "cn.jpush.android.CONTENT_TYPE";
	public static final String EXTRA_TITLE = "cn.jpush.android.TITLE";
	public static final String EXTRA_EXTRA = "cn.jpush.android.EXTRA";
	public static final String EXTRA_NOTIFICATION_ID = "cn.jpush.android.NOTIFICATION_ID";
	public static final String EXTRA_ACTIVITY_PARAM = "cn.jpush.android.ACTIVITY_PARAM";
	public static final String EXTRA_RICHPUSH_FILE_PATH = "cn.jpush.android.FILE_PATH";
	public static final String EXTRA_RICHPUSH_FILE_TYPE = "cn.jpush.android.FILE_TYPE";
	public static final String EXTRA_RICHPUSH_HTML_PATH = "cn.jpush.android.HTML_PATH";
	public static final String EXTRA_RICHPUSH_HTML_RES = "cn.jpush.android.HTML_RES";
	public static final String EXTRA_STATUS = "cn.jpush.android.STATUS";
	@Hide
	public static BA mba=null;
	@Hide
	public static String en;

	public void init(BA ba,String en1,boolean debug){
		mba=ba;
		JMsgWrapper.en=en1.toLowerCase(BA.cul);
		JMessageClient.init(BA.applicationContext);
		JMessageClient.setDebugMode(debug);
		JMessageClient.setNotificationMode(JMessageClient.NOTI_MODE_NO_NOTIFICATION);
		JMessageClient.registerEventReceiver(this);
	}
	@Hide
	public void onEvent(MessageEvent event){
		MessageWrapper im=new MessageWrapper();
		im.Initialize(event.getMessage());
		mba.raiseEvent(this, en+"_onjmsg", new Object[]{im});
		 
	}
	public void unRegisterEventReceiver(){
		JMessageClient.unRegisterEventReceiver(this);
	}
	@Hide
	public static class mbce extends BasicCallback {
		public String event;
		private BA mBa;
		public mbce(BA ba,String eventType) {
			super();
			mBa=ba;
			this.event = eventType.toLowerCase();
		}
		@Override
		public void gotResult(int paramInt, String paramString) {
			// TODO Auto-generated method stub
			mBa.raiseEvent(this, en+"_gotResult".toLowerCase(), new Object[]{event,paramInt,paramString,""});
		}
		
	}
	/**
	 * extVal=null
	 * @param username
	 * @param password
	 */
	public void register(String username, String password){
		JMessageClient.register(username, password, new mbce(mba, "register"));
	}
	/**
	 * extVal=null
	 * @param username
	 * @param password
	 */
	public void login(String username, String password){
		JMessageClient.login(username, password, new mbce(mba, "login"));
	}
	/**
	 * extVal=null
	 * @param username
	 * @param password
	 */
	public void logout(){
		JMessageClient.logout();
	}
	public void EnterSingleConversation(String targetid){
		JMessageClient.enterSingleConversation(targetid);
	}
	public void EnterGroupConversation(long grpId) {
		JMessageClient.enterGroupConversation(grpId);
	}
	public void ExitConversation() {
		JMessageClient.exitConversation();
	}
	/**
	 * got the result in callback event
	 * @param username
	 */
	public void getUserInfo(String username){
		JMessageClient.getUserInfo(username, new GetUserInfoCallback() {
			
			@Override
			public void gotResult(int responseCode, String responseMessage, UserInfo ui) {
				// TODO Auto-generated method stub
				JSONObject ob=new JSONObject();
				try {
					ob.put("ui", ui);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mba.raiseEvent(this, en+"_gotResult".toLowerCase(), new Object[]{"getuserinfo",responseCode,responseMessage,ob.toString()});
			}
		});
	}
	/**
	 * json string
	 */
	public String getMyInfo(){
		UserInfo ui= JMessageClient.getMyInfo();
		JSONObject jo=new JSONObject();
		try {
			jo.put("ui", ui);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jo.toString();
	}
	public void updateUserPassword(String oldPassword, String newPassword){
		JMessageClient.updateUserPassword(oldPassword, newPassword, new mbce(mba, "updateUserPassword"));
	}
	public void updateUserAvatar(String avatarPath){
		JMessageClient.updateUserAvatar(new File(avatarPath),  new mbce(mba, "updateUserAvatar"));
	}
	/**
	 * 
	 * @param username to send
	 * @param text to send
	 */
	public MessageWrapper createSingleTextMessage(String username, String text){
		MessageWrapper mw=new MessageWrapper();
		mw.Initialize(JMessageClient.createSingleTextMessage(username, text));
		return mw;
	}
	public MessageWrapper createGroupTextMessage(long groupID, String text){
		MessageWrapper mw=new MessageWrapper();
		mw.Initialize(JMessageClient.createGroupTextMessage(groupID, text));
		return mw;
	}
	/**
	 * if file not useable will return null
	 * @param username
	 * @param imageFile
	 */
	public MessageWrapper createSingleImageMessage(String username, String imageFile) {
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createSingleImageMessage(username, new File(imageFile)));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	public MessageWrapper createGroupImageMessage(long groupID, String imageFile) {
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createGroupImageMessage(groupID, new File(imageFile)));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	public MessageWrapper createSingleVoiceMessage(String username, 
			  String voiceFile, int duration){
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createSingleVoiceMessage(username, new File(voiceFile), duration));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	public MessageWrapper createGroupVoiceMessage(long groupID, 
			  String voiceFile, int duration){
		MessageWrapper mw=new MessageWrapper();
		try {
			mw.Initialize(JMessageClient.createGroupVoiceMessage(groupID, new File(voiceFile), duration));
			return mw;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mba.setLastException(e);
			return null;
		}
	}
	/**
	 * send the message object
	 * @param msg
	 */
	public void sendMessage(MessageWrapper msg) {
		JMessageClient.sendMessage(msg.getMessage());
	}
	/**
	 * 
	 * @return List<Conversation_im>
	 */
	public List getConversationList(){
		java.util.List<Conversation> lst=JMessageClient.getConversationList();
		List ret=new List();
		ret.Initialize();
		for(int i=0;i<lst.size();i++){
			ConversationWrapper cw=new ConversationWrapper();
			cw.Initialize(lst.get(i));
			ret.Add(cw);
		}
		return ret;
	}
	public ConversationWrapper getSingleConversation(String username){
		ConversationWrapper cw=new ConversationWrapper();
		cw.Initialize(JMessageClient.getSingleConversation(username));
		return cw;
	}
	public ConversationWrapper getGroupConversation(long GroupId){
		ConversationWrapper cw=new ConversationWrapper();
		cw.Initialize(JMessageClient.getGroupConversation(GroupId));
		return cw;
	}
	public void deleteSingleConversation(String username){
		JMessageClient.deleteSingleConversation(username);
	}
}
